from tkinter import *
from tkinter import ttk
from tkinter.messagebox import *
from pages.T10Page import *
from dev.serialList import *


class startPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量root
        self.root.geometry('%dx%d' % (400, 220))  # 设置窗口大小
        self.port = StringVar()
        self.baudrate = IntVar()
        self.devName = StringVar()
        self.baudrateRange = [9600, 19200, 38400, 57600, 115200]
        self.serialList = list_serial_ports()
        print(self.serialList)
        self.createPage()


    def createPage(self):
        self.page = Frame(self.root)  # 创建Frame
        self.page.pack()
        Label(self.page).grid(row=0, stick=W)
        # 设备型号选择
        Label(self.page, text='设备型号: ').grid(row=1, stick=W, pady=10)
        devComb = ttk.Combobox(self.page, textvariable=self.devName, values=('SD123-T10', 'ZSKJ-A8'), state='readonly', width=30)
        devComb.grid(row=1, column=1, stick=E)
        devComb.current(0)
        # 端口选择
        Label(self.page, text='端口: ').grid(row=2, stick=W, pady=10)
        portComb = ttk.Combobox(self.page, textvariable=self.port, values=self.serialList, state='readonly', width=30)
        portComb.grid(row=2, column=1, stick=E)
        portComb.current(0)
        # 波特率
        Label(self.page, text='波特率: ').grid(row=3, stick=W, pady=10)
        baudComb = ttk.Combobox(self.page, textvariable=self.baudrate, values=self.baudrateRange, state='readonly', width=30)
        baudComb.grid(row=3, column=1, stick=E)
        baudComb.current(0)
        # 确定按钮
        Button(self.page, text='连接设备', command=self.connect).grid(row=4, stick=W, pady=10)
        Button(self.page, text='历史数据', command=self.history).grid(row=4, column=1, stick=E)

    def connect(self):
        newPage = self.root
        newPage.devName = self.devName.get()
        newPage.port = self.port.get()
        newPage.baudrate = self.baudrate.get()
        if newPage.devName == 'SD123-T10':
            self.page.destroy()
            T10Page(self.root)
        elif newPage.devName == 'ZSKJ-A8':
            pass
            # self.page.destroy()
            # A8Page(self.root)
        else:
            showinfo(title='错误', message='填写信息错误！')
    def history(self):
        pass
        '''
        历史记录
        :return:
        
        self.page.destroy()
        HistoryPage(self.root)
        '''